package com.niit.blog.controller;

import com.google.common.base.Preconditions;
import com.niit.blog.convert.BlogClientConverter;
import com.niit.blog.entity.Dto.BlogClientDto;
import com.niit.blog.entity.Dto.SortDto;
import com.niit.blog.entity.domain.Blog;
import com.niit.blog.entity.resp.RedisResp;
import com.niit.blog.service.BlogService;
import com.niit.blog.util.MyResult;
import com.niit.blog.util.RedisUtil;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;
@Slf4j
@RestController
@RequestMapping("/sort")
public class SortController {
    @Resource
    private BlogService blogService;
    @Resource
    private RedisUtil redisUtil;

    //根据博客的点击量进行排行
    @PostMapping("/ByViews")
    public MyResult sortByBlogViews(){

        List<SortDto> list = blogService.prepareSortData();
        List<SortDto>  sortData =redisUtil.sortByViews(list);
        //返回数据封装
        List<RedisResp> redisResps = blogService.redisResponse(sortData);
           return MyResult.success(redisResps);
        }


        //根据博客的排行榜查询到对应的博客
    @PostMapping("/queryByBlogId")
    public MyResult<BlogClientDto> queryBlogById(@RequestParam("blogId") Integer blogId){

        Preconditions.checkNotNull(blogId,"查询条件不能为空");
        long l = blogId.longValue();
        Blog blogById = blogService.getBlogById(l);
        BlogClientDto blogClientDto = BlogClientConverter.INSTACE.convertBlogToBlogClientDto(blogById);
        log.info("查询的对象为:{}",blogClientDto);



        return MyResult.success(blogClientDto);
    }

}
